// ==UserScript==
// @name 10086gq
// @author wgu
// @description  10086gq
// @include http://f.10086.cn/*
// ==/UserScript==
gw.import('post.js',window);
(function($,jQuery){		
	
	$.net.config={
		'ckManage':gw.ckManage(["f.10086.cn/ganen","f.10086.cn/","f.10086.cn/getmore"])
	}
		
	var req=new XMLHttpRequest(),req2 = gw.req(),hostname=location.hostname;
	var panel=jQuery.net.cache,convert=gw.convert,ob=gw.ob,cookie=gw.cookie,ckManage=jQuery.net.config.ckManage;
	$.net.useInfo._init=function(obj){
		ckManage.remove();		
		var url='http://f.10086.cn/huc/user/ganen/login.do?m=submit&backurl=http://f.10086.cn/ganen/index.do&fr=ganen'
			,data='mobilenum={#userId}&password={#passWord}';
		req.open('POST', url, false);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(data.format(obj));
			
		req.open('GET', "http://f.10086.cn/getmore/", false);
		req.send(null);
		obj.cookie=ckManage.get();
		return this;
	}
	
	jQuery.net.panel._panel=function (x,button){
		var html=[],t1='<option value="{#i}">{#name}</option>';
		var useInfos=jQuery.net.cache.useInfos,x=x||"";
		for(var i in useInfos){
			if(useInfos[i].name==x){
				html.unshift(t1.format({'i':i,'name':useInfos[i].name}));
			}else{
				html.push(t1.format({'i':i,'name':useInfos[i].name}));
			}
			jQuery.net.reqInfos[useInfos[i].userId]={'time':0,'req':new XMLHttpRequest()};
		}

		if(jQuery("#wgu_tool").length==0){
			jQuery('<div id="wgu_tool" style="bottom:0px;border-width: 1px;position: fixed;border-style: solid;border-radius: 11px 11px 0px 0px;padding:5px;background-color:White;">'
				+'<div><select id="wgu_sel" >'+html.join('')+'</select></div>'
				+'<div>welcome to my tools! welcome to my tools! </div>'
				+'</div>').appendTo('body');
		}else{
			jQuery("#wgu_tool").html('<div><select id="wgu_sel" >'+html.join('')+'</select></div>'
				+'<div>welcome to my tools! welcome to my tools! </div>');
		}
		jQuery('#wgu_sel').bind('change',function (){
				var i=this.value;
				jQuery.net.config.ckManage.remove().set(useInfos[i]['cookie']);
			});
			
		var temp=jQuery('<div></div>').appendTo('#wgu_tool');
		button.forEach(function(v,i){
			$('<button>'+v.name+'</button>').appendTo(temp).bind('click',v);
		});
	
		return this;
	}
	
	if($.isEmptyObject(panel.useInfos)){
		$.net.panel
			._add({'userId':'15850116859','passWord':'1118860202wgu','name':'15850116859'})
			._add({'userId':'13584865387','passWord':'1118860202wgu','name':'13584865387'})
			._add({'userId':'13405076133','passWord':'1118860202wgu','name':'13405076133'})
			._add({'userId':'13812646820','passWord':'1118860202wgu','name':'13812646820'})
			._add({'userId':'13814842597','passWord':'69332826XX','name':'13814842597'})
			
	}

	function send(){
		var useInfos=panel.useInfos;


		gw.ob.add('request','http://f.10086.cn/ganen/yao.do*','get','wgu','Cookie');
		gw.ob.add('request','http://f.10086.cn/ganen/yao.do*','get','wguR','Referer');
		
		gw.ob.add('request','http://f.10086.cn/ganen/tosend.do?toAll=yes','get','wgu','Cookie');
		gw.ob.add('request','http://f.10086.cn/ganen/tosend.do?toAll=yes','get','wguR','Referer');
		
		for(var i in useInfos){
			pp(useInfos[i])();
		}
	}
	
	function pp(useInfo){
		var reqInfo=jQuery.net.reqInfos[useInfo.userId];
		var req=reqInfo['req'];
		var time=0;
		reqInfo.step=1;
		req.addEventListener("load", function(){
			var t=1000;
			if (this.readyState == 4) {
				if(reqInfo.step==1){
					useInfo['url']="http://f.10086.cn/ganen/"+req.responseText.match(/yao.do\?s=[^"]+/g)[0];
					reqInfo.step=2;
				}else if(reqInfo.step==2||reqInfo.step==4){
					useInfo['rid']=/postfield name="r" value="(\d+)"/g.exec(req.responseText)[1]; 
					reqInfo.step=3;
				}else if(reqInfo.step==3){
					useInfo['url']="http://f.10086.cn/ganen/"+req.responseText.match(/yao.do\?s=[^"]+/g)[0];
					reqInfo.step=4;
				}
				setTimeout(_p,t);
			}
		}, true);
		
		function _p(){
			if(reqInfo.step==1){
				req.open('GET', 'http://f.10086.cn/ganen/index.do', true);
				req.setRequestHeader('wgu',ckManage.toString(useInfo['cookie']));
				req.send(null);
			}else if(reqInfo.step==2){
				req.open('GET',useInfo['url'], true);
				req.setRequestHeader('wgu',ckManage.toString(useInfo['cookie']));
				req.setRequestHeader('wguR','http://f.10086.cn/ganen/index.do');
				req.send(null);
			}else if(reqInfo.step==3){
				req.open('POST','http://f.10086.cn/ganen/tosend.do?toAll=yes', true);
				req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				req.setRequestHeader('wgu',ckManage.toString(useInfo['cookie']));
				req.setRequestHeader('wguR',useInfo['url']);
				req.send('type=&zhid=&id=&r='+useInfo['rid']);
			}else if(reqInfo.step==4){
				req.open('GET',useInfo['url'], true);
				req.setRequestHeader('wgu',ckManage.toString(useInfo['cookie']));
				req.setRequestHeader('wguR','http://f.10086.cn/ganen/sendmsgover.jsp');
				req.send(null);
			}
		}
		return _p;
	}
	
	function reload(){
		setInterval(_reload,1000*60*30);
	}
	
	function _reload(){
		$.net.panel
			._add({'userId':'15850116859','passWord':'1118860202wgu','name':'15850116859'})
			._add({'userId':'13584865387','passWord':'1118860202wgu','name':'13584865387'})
			._add({'userId':'13405076133','passWord':'1118860202wgu','name':'13405076133'})
			._add({'userId':'13812646820','passWord':'1118860202wgu','name':'13812646820'})
			._add({'userId':'13814842597','passWord':'69332826XX','name':'13814842597'})
			._add({'userId':'13962477220','passWord':'xb55627197','name':'13962477220'})
	}
	$.net.panel._panel("",[send,reload]);	
	
})(___jQuery,___jQuery)